package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.Setmeal;
import com.zrrd.reggie.bean.SetmealDish;
import com.zrrd.reggie.bean.SetmealDto;
import com.zrrd.reggie.mapper.SetmealDishMapper;
import com.zrrd.reggie.mapper.SetmealMapper;
import com.zrrd.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {


    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    @Transactional
    public void saveSetmeal(SetmealDto dto){
        //调用本类的方法 保存套餐基本数据
        this.save(dto);
        //获取前端传递过来的选择的菜品列表
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            //设置套餐id
            setmealDish.setSetmealId(dto.getId());
            //保存
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDto dto) {

        //直接修改套餐表的数据
        this.updateById(dto);

        //条件构造器
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", dto.getId());
        //执行条件删除
        setmealDishMapper.delete(wrapper);

        //获取前端传递过来的选择的菜品列表
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //遍历
        for (SetmealDish setmealDish : setmealDishes) {

            setmealDish.setSetmealId(dto.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }


}
